package com.example.lenovo.downloaddemo.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;
import com.example.lenovo.downloaddemo.http.okhttp.utils.ToastUtil;

import java.io.File;

public class InitApkBroadCastReceiver extends BroadcastReceiver {
    private void deleteSingleFile(String filePath$Name) {
        File file = new File(filePath$Name);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                Log.e("--Method--", "Copy_Delete.deleteSingleFile: 删除单个文件" + filePath$Name + "成功！");
            } else {
                Log.e("---删除","删除单个文件" + filePath$Name + "失败！");
            }
        } else {
            Log.e("---删除","删除单个文件" + filePath$Name + "不存在！");
        }
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
            System.out.println("监听到系统广播添加");
            deleteSingleFile(AppContext.getInstance().getExternalFilesDir(null)+"/download/test.apk");
        }

        if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
            System.out.println("监听到系统广播移除");
            deleteSingleFile(AppContext.getInstance().getExternalFilesDir(null)+"/download/test.apk");
        }

        if (Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) {
            System.out.println("监听到系统广播替换");
            deleteSingleFile(AppContext.getInstance().getExternalFilesDir(null)+"/download/test.apk");
        }
    }
}
